#include "MyScenery.h"
#include "MyInfobar.h"

MyScenery::MyScenery(MyGame* _this)
{
	parent = _this;
	load_imgs();
	this->total_width_bg = backSheet->w;
	this->total_width_sky = skySheet->w;
	this->offset_bg = total_width_bg;
	this->off_sky_1 = 0;
	this->off_sky_2 = 0;
	this->scrolling = true;
	this->still_first = true;
}
MyScenery::~MyScenery()
{
	SDL_FreeSurface(backSheet);
	SDL_FreeSurface(skySheet);
}
void MyScenery::load_imgs()
{
	#ifdef _WIN32
	backSheet = load_image("misc\\whole_bg.png");
	skySheet = load_image("misc\\sky.jpg");
	#else
	backSheet = load_image("misc/whole_bg.png");	
	skySheet = load_image("misc/sky.jpg");
	#endif

	//instead of loading images (can be used as well) we make this layers to be faded out
	//skySheet = SDL_CreateRGBSurface(parent->screen->flags, SKY_W, SKY_H, SCREEN_BPP, RMASK, GMASK, BMASK, AMASK);
	//make_fadeout_layer(skySheet,make_color(71,138,220),make_color(19,55,100));
	//floorSheet = SDL_CreateRGBSurface(parent->screen->flags, FIELD_W, FIELD_H, SCREEN_BPP, RMASK, GMASK, BMASK, AMASK);
	//make_fadeout_layer(floorSheet,make_color(14,105,30),make_color(27,207,58));

    //If there was a problem in loading image
	if ((!backSheet)||(!skySheet)) 
	{	
			printf("ERROR:Cannot load game images!\n (check files bg_whole.jpg and sky.jpg");
			parent->demand_quit();
	}	    
}

void MyScenery::draw_scenery(int _t)
{	
	SDL_Rect *tmp1, *tmp2;
	if (scrolling)
	{
		this->add_offset_sky(_t/20);	
	}

	//first screen is first
	if (still_first)
	{
		//second screen also visible
		if (this->off_sky_2) 
		{
			tmp1=make_rect(off_sky_1, 0, total_width_sky - off_sky_1, skySheet->h);
			SDL_BlitSurface(skySheet,tmp1,parent->parent->screen,0);
			delete(tmp1);
			SDL_BlitSurface(skySheet,tmp1=make_rect(0, 0, off_sky_2, skySheet->h),parent->parent->screen,tmp2=make_rect(total_width_sky-off_sky_1,0,off_sky_2, skySheet->h));
			delete(tmp1);
			delete(tmp2);
		}
		//only first visible
		else
		{
			SDL_BlitSurface(skySheet,tmp1=make_rect(off_sky_1, 0, SCREEN_W, skySheet->h),parent->parent->screen,0);
			delete(tmp1);
		}
	}
	//second screen is first
	else
	{
		//first screen also visible
		if (this->off_sky_1) 
		{
			SDL_BlitSurface(skySheet,tmp1=make_rect(off_sky_2, 0, skySheet->w - off_sky_2, skySheet->h),parent->parent->screen,0);
			delete(tmp1);
			SDL_BlitSurface(skySheet,tmp1=make_rect(0, 0, off_sky_1, skySheet->h),parent->parent->screen,tmp2=make_rect(total_width_sky - off_sky_2,0,off_sky_1, skySheet->h));
			delete(tmp1);
			delete(tmp2);
		}
		//only second visible
		else
		{
			SDL_BlitSurface(skySheet,tmp1=make_rect(off_sky_2, 0, SCREEN_W, skySheet->h),parent->parent->screen,0);
			delete(tmp1);
		}
	}

	//draw bg	
	SDL_BlitSurface(backSheet,tmp1 = make_rect(offset_bg-SCREEN_W, 0, SCREEN_W, backSheet->h),parent->parent->screen,0); 	
	delete(tmp1);
}
void MyScenery::add_offset_bg(int _offset)
{	
	this->offset_bg += _offset;
	if (this->offset_bg > this->total_width_bg)
	{
		this->offset_bg = this->total_width_bg;
	}
}
//important to both screen offsets correctly

void MyScenery::add_offset_sky(int _offset)
{
	//off_sky_1 is first
	if (still_first)
	{
		off_sky_1 += _offset;
		//if second screen visible
		if (off_sky_2)
		{
			off_sky_2 += _offset;
		}
		//first screen has gap at the end
		if ((off_sky_1 + SCREEN_W >= total_width_sky) && !off_sky_2)
		{	
			off_sky_2 = off_sky_1 -(total_width_sky - SCREEN_W);			
		}
		//first screen completely out
		if (off_sky_1 > total_width_sky)
		{
			off_sky_1 = 0;
			off_sky_2 = 0;
			still_first = false;
		}		
	}
	//off_sky_2 is first
	else
	{
		off_sky_2 += _offset;
		//if first screen visible
		if (off_sky_1)
		{
			off_sky_1 += _offset;
		}
		//second screen just gone out of visible screen rectangle
		if ((off_sky_2 + SCREEN_W >= total_width_sky)&& !off_sky_1)
		{			
			off_sky_1 = off_sky_2 - (total_width_sky - SCREEN_W);			
		}
		if (off_sky_2 > total_width_sky)
		{
			off_sky_1 = 0;
			off_sky_2 = 0;
			still_first = true;
		}		
	}
}